package com.sirenbang.easyshop.service.impl;

import java.util.List;

import com.sirenbang.easyshop.dao.GoodsDao;
import com.sirenbang.easyshop.dao.impl.GoodsDaoJDBCImpl;
import com.sirenbang.easyshop.domain.Goods;
import com.sirenbang.easyshop.exception.ClientException;
import com.sirenbang.easyshop.service.GoodsService;
import com.sirenbang.easyshop.utils.DBUtils;
import com.sirenbang.easyshop.utils.TransactionManager;

public class GoodsServiceImpl implements GoodsService {

	public void add(Goods goods) {
		
		GoodsDao dao=new GoodsDaoJDBCImpl();
		TransactionManager manager=DBUtils.getInstance().getTransactionManager();
		
		manager.beginTransaction();
		try{
			dao.add(goods);
			manager.commitAndClose();
		}catch(ClientException e){
			manager.rollbackAndClose();
			throw new ClientException(e.getMessage());
		}
		
	}

	public List<Goods> loadAll() {
		
		GoodsDao dao=new GoodsDaoJDBCImpl();
		TransactionManager manager=DBUtils.getInstance().getTransactionManager();
		List<Goods> goodsList=null; 
		
		manager.beginTransaction();
		try{
			goodsList=dao.loadAll();
			manager.commitAndClose();
		}catch(ClientException e){
			manager.rollbackAndClose();
			throw new ClientException(e.getMessage());
		}
		
		return goodsList;
		
	}

	public byte[] getPic(Integer goodsNo) {
		
		GoodsDao dao=new GoodsDaoJDBCImpl();
		TransactionManager manager=DBUtils.getInstance().getTransactionManager();
		byte[] goodsPic=null;
		
		manager.beginTransaction();
		try{
			goodsPic=dao.getPic(goodsNo);
			manager.commitAndClose();
		}catch(ClientException e){
			manager.rollbackAndClose();
			throw new ClientException(e.getMessage());
		}
		
		return goodsPic;
		
	}

}
